package com.youlai.boot.common.controller;

import com.youlai.boot.common.excel.ExcelOperationService;
import com.youlai.boot.common.result.ExcelResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Excel 操作控制器
 * 
 * @author Jason
 * @since 2024/05/18
 */
@Tag(name = "Excel 操作接口", description = "提供统一的Excel导入导出功能")
@RestController
@RequestMapping("/api/excel")
@RequiredArgsConstructor
@Slf4j
public class ExcelController {
    
    private final ExcelOperationService excelOperationService;
    
    @Operation(
        summary = "导出Excel", 
        description = "根据模块标识和查询参数导出Excel文件",
        tags = {"excel", "export"}
    )
    @ApiResponses(value = {
        @ApiResponse(
            responseCode = "200", 
            description = "导出成功，返回Excel文件流",
            content = @Content(mediaType = MediaType.APPLICATION_OCTET_STREAM_VALUE)
        ),
        @ApiResponse(
            responseCode = "500", 
            description = "导出失败",
            content = @Content
        )
    })
    @GetMapping("/export/{module}")
    public void exportExcel(
            @Parameter(description = "模块标识，例如：bigcap") @PathVariable String module,
            @Parameter(description = "查询参数，支持各种过滤条件") @RequestParam Map<String, Object> params,
            HttpServletResponse response
    ) {
        excelOperationService.exportExcel(params, module, response);
    }
    
    @Operation(
        summary = "导入Excel", 
        description = "根据模块标识导入Excel文件数据",
        tags = {"excel", "import"}
    )
    @ApiResponses(value = {
        @ApiResponse(
            responseCode = "200", 
            description = "导入成功，返回导入结果",
            content = @Content(
                mediaType = MediaType.APPLICATION_JSON_VALUE,
                schema = @Schema(implementation = Result.class)
            )
        ),
        @ApiResponse(
            responseCode = "400", 
            description = "导入失败，文件格式错误",
            content = @Content
        ),
        @ApiResponse(
            responseCode = "500", 
            description = "导入失败，服务器错误",
            content = @Content
        )
    })
    @PostMapping("/import/{module}")
    public Result<ExcelResult> importExcel(
            @Parameter(description = "模块标识，例如：bigcap") @PathVariable String module,
            @Parameter(
                description = "Excel文件，仅支持.xlsx格式", 
                required = true,
                content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)
            ) 
            @RequestParam("file") MultipartFile file
    ) {
        ExcelResult result = excelOperationService.importExcel(file, module);
        return Result.success(result);
    }
    
    @Operation(
        summary = "下载Excel模板", 
        description = "根据模块标识下载Excel导入模板",
        tags = {"excel", "template"}
    )
    @ApiResponses(value = {
        @ApiResponse(
            responseCode = "200", 
            description = "下载成功，返回Excel文件流",
            content = @Content(mediaType = MediaType.APPLICATION_OCTET_STREAM_VALUE)
        ),
        @ApiResponse(
            responseCode = "404", 
            description = "模板不存在",
            content = @Content
        ),
        @ApiResponse(
            responseCode = "500", 
            description = "下载失败，服务器错误",
            content = @Content
        )
    })
    @GetMapping("/template/{module}")
    public void downloadTemplate(
            @Parameter(description = "模块标识，例如：bigcap") @PathVariable String module,
            HttpServletResponse response
    ) {
        excelOperationService.downloadTemplate(module, response);
    }
} 